package com.qx.chat.system.event;

import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.query.Update;

import com.qx.chat.system.dao.AbstractRepository;
import com.qx.chat.system.dao.DeptUserDaoImpl;
import com.qx.chat.system.dao.GroupDaoImpl;
import com.qx.chat.system.dao.GroupUserDaoImpl;
import com.qx.chat.system.dao.UserDaoImpl;
import com.qx.chat.system.entity.DeptUser;
import com.qx.chat.system.entity.Group;
import com.qx.chat.system.entity.GroupUser;
import com.qx.chat.system.entity.User;
import com.qx.chat.system.util.SpringUtils;
import com.qx.chat.websocket.interceptor.QxWebSocketHandshakeInterceptor;


/**
 * 群组自主行为相关事件
 * 
 * @author cyf
 * 
 */
public class GroupEvent {

	/**
	 * 编辑分组
	 * 
	 * @author cyf
	 * 
	 */
	public static class GroupEditEvent extends Event implements Serializable {

		private static final long serialVersionUID = -7430185558396236914L;
		private static final Logger LOGGER = LoggerFactory.getLogger(QxWebSocketHandshakeInterceptor.class);
		public final static String EDITTYPE = "GroupEdit";
		public final static String ADDMEMBERTYPE = "GroupAddMember";
		public final static String DELTYPE = "GroupDel";
		public final static String DELMEMBERTYPE = "GroupDelMember";
		private String loginName;
		private String groupId;;
		private String groupName;
		private String errorMsg;
		public GroupEditEvent(String loginName,String groupId,String groupName) {
			super();
			this.loginName = loginName;
			this.groupId = groupId;
			this.groupName = groupName;
		}
		//新增或编辑分组
		public void createOrUpdate(){
			super.setType(EDITTYPE);
			AbstractRepository<User> userService = SpringUtils.getBean(UserDaoImpl.class);
			User user = (User) userService.findOne("loginName", loginName);
			if(user!=null){
				AbstractRepository<Group> groupService = SpringUtils.getBean(GroupDaoImpl.class);
				Group group = groupService.findById(groupId);
				if(group == null){
					AbstractRepository<DeptUser> deptUserService = SpringUtils.getBean(DeptUserDaoImpl.class);
					DeptUser du = (DeptUser) deptUserService.findOne("user", user.getId());
					group = new Group();
					group.setName(groupName);
					group.setOwner(user);
					group.initData(user, du.getDept());
					groupService.insert(group);
					AbstractRepository<GroupUser> groupUserService = SpringUtils.getBean(GroupUserDaoImpl.class);
					GroupUser gu = new GroupUser();
					gu.setUser(user);
					gu.setGroup(group);
					gu.setOrder(0);
					groupUserService.insert(gu);
					LOGGER.info(user.getNickname()+"创建群组："+groupName);
				}else{
					groupService.findAndModify(group.getId(), new Update().set("name", groupName));
				}
			}else{
				this.errorMsg = "群组编辑失败";
			}
		}
		public String getLoginName() {
			return loginName;
		}
		public void setLoginName(String loginName) {
			this.loginName = loginName;
		}
		public String getGroupId() {
			return groupId;
		}
		public void setGroupId(String groupId) {
			this.groupId = groupId;
		}
		public String getGroupName() {
			return groupName;
		}
		public void setGroupName(String groupName) {
			this.groupName = groupName;
		}
		public String getErrorMsg() {
			return errorMsg;
		}
		public void setErrorMsg(String errorMsg) {
			this.errorMsg = errorMsg;
		}
	}
}
